package com.ruoyi.hikvision.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.hikvision.domain.HikvisionDevice;
import com.ruoyi.hikvision.domain.HikvisionDeviceFace;
import com.ruoyi.hikvision.domain.HikvisionDownloadFace;
import com.ruoyi.hikvision.service.IHikvisionDeviceFaceService;
import com.ruoyi.hikvision.service.IHikvisionDeviceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hikvision.domain.HikvisionFace;
import com.ruoyi.hikvision.service.IHikvisionFaceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人脸管理Controller
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/hikvision/face")
public class HikvisionFaceController extends BaseController {
    @Autowired
    private IHikvisionFaceService hikvisionFaceService;

    @Autowired
    private IHikvisionDeviceService hikvisionDeviceService;

    @Autowired
    private IHikvisionDeviceFaceService hikvisionDeviceFaceService;

    /**
     * 查询人脸管理列表
     */
    @PreAuthorize("@ss.hasPermi('hikvision:face:list')")
    @GetMapping("/list")
    public TableDataInfo list(HikvisionFace hikvisionFace) {
        startPage();
        List<HikvisionFace> list = hikvisionFaceService.selectHikvisionFaceList(hikvisionFace);
        return getDataTable(list);
    }

    /**
     * 导出人脸管理列表
     */
    @PreAuthorize("@ss.hasPermi('hikvision:face:export')")
    @Log(title = "人脸管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HikvisionFace hikvisionFace) {
        List<HikvisionFace> list = hikvisionFaceService.selectHikvisionFaceList(hikvisionFace);
        ExcelUtil<HikvisionFace> util = new ExcelUtil<HikvisionFace>(HikvisionFace.class);
        util.exportExcel(response, list, "人脸管理数据");
    }

    /**
     * 获取人脸管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('hikvision:face:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hikvisionFaceService.selectHikvisionFaceById(id));
    }

    /**
     * 新增人脸管理
     */
    @PreAuthorize("@ss.hasPermi('hikvision:face:add')")
    @Log(title = "人脸管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HikvisionFace hikvisionFace) {
        return toAjax(hikvisionFaceService.insertHikvisionFace(hikvisionFace));
    }

    /**
     * 修改人脸管理
     */
    @PreAuthorize("@ss.hasPermi('hikvision:face:edit')")
    @Log(title = "人脸管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HikvisionFace hikvisionFace) {
        return toAjax(hikvisionFaceService.updateHikvisionFace(hikvisionFace));
    }

    /**
     * 删除人脸管理
     */
    @PreAuthorize("@ss.hasPermi('hikvision:face:remove')")
    @Log(title = "人脸管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hikvisionFaceService.deleteHikvisionFaceByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('hikvision:face:edit')")
    @Log(title = "人脸下发", businessType = BusinessType.OTHER)
    @PostMapping("/download")
    public AjaxResult download(@RequestBody HikvisionDownloadFace downloadFace) {
        List<HikvisionDevice> deviceList = hikvisionDeviceService.selectHikvisionDeviceByIds(downloadFace.getDeviceId());
        List<HikvisionFace> faceList = hikvisionFaceService.selectHikvisionFaceByIds(downloadFace.getFaceId());

        // 写入数据库
        for (HikvisionDevice deviceInfo : deviceList) {
            for (HikvisionFace faceInfo : faceList) {
                HikvisionDeviceFace hikvisionDeviceFace = HikvisionDeviceFace.builder()
                        .deviceId(deviceInfo.getId())
                        .facecode(faceInfo.getFacecode())
                        .build();
                HikvisionDeviceFace deviceFace = hikvisionDeviceFaceService.selectHikvisionDeviceFace(hikvisionDeviceFace);

                if (deviceFace != null) {
                    deviceFace.setComId(faceInfo.getComId());
                    deviceFace.setFacecode(faceInfo.getFacecode());
                    deviceFace.setPersonType(faceInfo.getPersonType());
                    deviceFace.setUuid(faceInfo.getUuid());
                    deviceFace.setName(faceInfo.getName());
                    deviceFace.setSex(faceInfo.getSex());
                    deviceFace.setFace(faceInfo.getFace());
                    deviceFace.setStatus(1);
                    deviceFace.setStatusOld(1);
                    deviceFace.setIcNumber(faceInfo.getIcNumber());
                    deviceFace.setDeviceId(deviceInfo.getId());
                    deviceFace.setDeviceNo(deviceInfo.getDeviceNo());
                    deviceFace.setIdcard(faceInfo.getIdcard());
                    deviceFace.setMobile(faceInfo.getMobile());
                    deviceFace.setJobNumber(faceInfo.getJobNumber());
                    hikvisionDeviceFaceService.updateHikvisionDeviceFace(deviceFace);
                } else {
                    hikvisionDeviceFace.setComId(faceInfo.getComId());
                    hikvisionDeviceFace.setFacecode(faceInfo.getFacecode());
                    hikvisionDeviceFace.setPersonType(faceInfo.getPersonType());
                    hikvisionDeviceFace.setUuid(faceInfo.getUuid());
                    hikvisionDeviceFace.setName(faceInfo.getName());
                    hikvisionDeviceFace.setSex(faceInfo.getSex());
                    hikvisionDeviceFace.setFace(faceInfo.getFace());
                    hikvisionDeviceFace.setStatus(1);
                    hikvisionDeviceFace.setStatusOld(1);
                    hikvisionDeviceFace.setIcNumber(faceInfo.getIcNumber());
                    hikvisionDeviceFace.setDeviceId(deviceInfo.getId());
                    hikvisionDeviceFace.setDeviceNo(deviceInfo.getDeviceNo());
                    hikvisionDeviceFace.setIdcard(faceInfo.getIdcard());
                    hikvisionDeviceFace.setMobile(faceInfo.getMobile());
                    hikvisionDeviceFace.setJobNumber(faceInfo.getJobNumber());
                    hikvisionDeviceFaceService.insertHikvisionDeviceFace(hikvisionDeviceFace);
                }
            }
        }
        return success("下发成功");
    }
}
